package com.xbq.exceleditor.ui

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ktx.immersionBar
import com.xbq.exceleditor.R
import com.xbq.exceleditor.databinding.ActivityFeedbackBinding
import com.xbq.exceleditor.utils.validateEmpty
import com.xbq.exceleditor.utils.value
import com.xbq.xbqsdk.component.activity.VBActivity
import com.xbq.xbqsdk.net.common.CommonApi
import com.xbq.xbqsdk.net.common.dto.AddFeedbackDto
import com.xbq.xbqsdk.util.click.onDebouncedClick
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: liaohaiping
 * Time: 2020-10-30
 * Description:
 */
@AndroidEntryPoint
class FeedbackActivity : VBActivity<ActivityFeedbackBinding>() {
    @Inject
    lateinit var commonApi: CommonApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            statusBarView(R.id.statusbar)
        }
        binding.adview.init("feedback",this)
        binding.btnBack.onDebouncedClick { finish() }
        binding.btnSubmit.onDebouncedClick { submitFeedback() }

    }

    private fun submitFeedback() {
        val title = binding.etTitle.value()
        val content = binding.etContent.value()
        val contactPhone = binding.etContactPhone.value()
        if (validateEmpty(title, R.string.pls_type_feedback_title)
            && validateEmpty(content, R.string.pls_type_feedback_content)
        ) {
            lifecycleScope.launch {
                val res = commonApi.addFeedback(AddFeedbackDto(title, content,contactPhone))
                if(res.success()){
                    ToastUtils.showShort(R.string.submit_feedback_success)
                    finish()
                }else{
                    ToastUtils.showShort(res.message)
                }
            }
        }
    }
}